package io.renren.modules.yckj.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.constant.Constant;
import io.renren.common.page.PageData;
import io.renren.common.utils.ExcelUtils;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.yckj.dto.ProjectDTO;
import io.renren.modules.yckj.enums.status.CommonStatus;
import io.renren.modules.yckj.excel.ProjectExcel;
import io.renren.modules.yckj.service.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 检测项目表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2025-04-16
 */
@RestController
@RequestMapping("yckj/project")
@Tag(name = "检测项目表")
public class ProjectController {
    @Autowired
    private ProjectService projectService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @Parameters({
            @Parameter(name = Constant.PAGE, description = "当前页码，从1开始", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.LIMIT, description = "每页显示记录数", in = ParameterIn.QUERY, required = true, ref = "int"),
            @Parameter(name = Constant.ORDER_FIELD, description = "排序字段", in = ParameterIn.QUERY, ref = "String"),
            @Parameter(name = Constant.ORDER, description = "排序方式，可选值(asc、desc)", in = ParameterIn.QUERY, ref = "String")

    })
    @RequiresPermissions("yckj:project:page")
    public Result<PageData<ProjectDTO>> page(@RequestParam Map<String, Object> params) {
        PageData<ProjectDTO> page = projectService.page(params);

        return new Result<PageData<ProjectDTO>>().ok(page);
    }

    @GetMapping("list")
    @Operation(summary = "列表")
//    @RequiresPermissions("yckj:machine:page")
    public Result<List<ProjectDTO>> list() {
        HashMap<String, Object> map = new HashMap<>(1);
        map.put("status", CommonStatus.YES.getCode());
        List<ProjectDTO> data = projectService.list(map);

        return new Result<List<ProjectDTO>>().ok(data);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
    @RequiresPermissions("yckj:project:info")
    public Result<ProjectDTO> get(@PathVariable("id") Long id) {
        ProjectDTO data = projectService.get(id);

        return new Result<ProjectDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @LogOperation("保存")
    @RequiresPermissions("yckj:project:save")
    public Result save(@RequestBody ProjectDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);
        dto.setCreateTime(new Date());
        dto.setUpdateTime(new Date());
        dto.setContent(dto.getContent().replace("\n", ""));

        projectService.save(dto);

        return new Result();
    }

    @PutMapping
    @Operation(summary = "修改")
    @LogOperation("修改")
    @RequiresPermissions("yckj:project:update")
    public Result update(@RequestBody ProjectDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);
        dto.setUpdateTime(new Date());
        dto.setContent(dto.getContent().replace("\n", ""));

        projectService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @LogOperation("删除")
    @RequiresPermissions("yckj:project:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        projectService.delete(ids);

        return new Result();
    }

    @GetMapping("export")
    @Operation(summary = "导出")
    @LogOperation("导出")
    @RequiresPermissions("yckj:project:export")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<ProjectDTO> list = projectService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "检测项目表", list, ProjectExcel.class);
    }

}